﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GsOrganization.Common.Extend
{
    public static class StringExtend
    {
        public static bool IsNullOrEmpty(this string SrcText) => string.IsNullOrEmpty(SrcText);

        public static string ToPureString(this string SrcText)
        {
            if (SrcText.IsNullOrEmpty())
                return null!;
            return SrcText.Trim(" \r\n\t\0".ToCharArray());
        }

        /// <summary>
        /// 转为GPS经度信息
        /// </summary>
        /// <param name="SrcText"></param>
        /// <returns></returns>
        public static double StringToLng(this string SrcText)
        {
            if (SrcText.IsNullOrEmpty())
                return 0d;
            return SrcText.ToPureString().Substring(0, 3).StringToDouble() + SrcText.ToPureString().Remove(0, 3).StringToDouble() / 60d;
        }

        /// <summary>
        /// 转为GPS纬度信息
        /// </summary>
        /// <param name="SrcText"></param>
        /// <returns></returns>
        public static double StringToLat(this string SrcText)
        {
            if (SrcText.IsNullOrEmpty())
            {
                return 0d;
            }
            return SrcText.ToPureString().Substring(0, 2).StringToDouble() + SrcText.ToPureString().Remove(0, 2).StringToDouble() / 60d;
        }

        /// <summary>
        /// 转为GPS协议中的时间信息
        /// </summary>
        /// <param name="SrcText"></param>
        /// <returns></returns>
        public static DateTime StringToGPSDateTime(this string SrcText)
        {
            DateTime dt = DateTime.UtcNow.Date
                .AddHours(SrcText.Substring(0, 2).StringToInt())
                .AddMinutes(SrcText.Substring(2, 2).StringToInt())
                .AddSeconds(SrcText.Remove(0, 4).StringToDouble());
            if (dt.Hour > 16)
                dt = dt.AddHours(8).AddDays(-1);
            else
                dt = dt.AddHours(8);
            return dt;
        }

        /// <summary>
        /// 字符串转双精度小数
        /// </summary>
        /// <param name="SrcText"></param>
        /// <returns></returns>
        public static double StringToDouble(this string SrcText)
        {
            double doubleText = 0;
            double.TryParse(SrcText, out doubleText);
            return doubleText;
        }

        /// <summary>
        /// 字符串转整数
        /// </summary>
        /// <param name="SrcText"></param>
        /// <returns></returns>
        public static int StringToInt(this string SrcText)
        {
            int intText = 0;
            int.TryParse(SrcText,out intText);
            return intText;
        }

        public static byte[] FromHexString(this string SrcText)
        {
            if (SrcText == null)
            {
                return null;
            }
            if (SrcText.Length % 2 != 0)
            {
                return null;
            }
            byte[] bsData = new byte[SrcText.Length / 2];
            for (int i = 0; i < bsData.Length; i++)
            {
                bsData[i] = Convert.ToByte($"{SrcText[i * 2]}{SrcText[i * 2 + 1]}", 16);
            }
            return bsData;

        }
    }
}
